layui.define(['form','table'], function(exports){
  var $ = layui.$,form = layui.form,table = layui.table;

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/agent/level/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'num', title: '级别'}
      ,{field: 'name', title: '级别名称'}
	  ,{field: 'serviceChargeRatio', title: '服务费比例',templet:function (record){
           return record.serviceChargeRatio + "%";
       }}
      ,{field: 'updateDate', title: '修改时间'}
      ,{field: 'createDate', title: '创建时间'}
      ,{title: '操作', width: 200, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: false
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
    });
  });
  
  //事件
  var active = {
    add: function(){
      layer.open({
        type: 2
        ,title: '新增用户级别'
        ,content: layui.setter.webRoot + '/agent/level/add.html'
        ,maxmin: false
        ,area: ['500px', '400px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submitID = 'LAY-form-submit'
          ,submit = layero.find('iframe').contents().find('#'+ submitID);
          iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
            var field = data.field;
        	$.ajax({
	    		type: "post",
	            url: layui.setter.webRoot + '/agent/level/add.do',
	            data:field,
				dataType:"json",
				success:function(res){
					if(res.code == 200){
						table.reload('LAY-table');
						layer.close(index);
					}else{
						layer.msg(res.msg,{icon: 2});
					}
				}
	        });
          });  
          submit.trigger('click');
        }
      }); 
    }
    ,update: function(obj){
          layer.open({
              type: 2
              ,title: '修改代理级别'
              ,content: layui.setter.webRoot + '/agent/level/update.html?num='+obj.data.num
              ,maxmin: false
              ,area: ['500px', '400px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/agent/level/update.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  layer.close(index);
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,jumpInline: function(obj){
          layer.open({
              type: 2
              ,title: '插入用户级别'
              ,content: layui.setter.webRoot + '/agent/level/jump/add.html?num='+obj.data.num
              ,maxmin: false
              ,area: ['500px', '400px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/agent/level/jump/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  table.reload('LAY-table');
                                  layer.close(index);
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
     ,del:function(obj){
    	layer.confirm('确定要删除该用户级别吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/agent/level/del.do',
	          data:{num:obj.data.num},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
                    layer.msg('删除成功',{icon: 1});
                    table.reload('LAY-table');
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
    }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('agent_level_list', {})
});